package com.dsg.service.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsg.common.security.SystemAuthorizingUser;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.dao.account.DsgAccountCardMapper;
import com.dsg.dao.apply.DsgApplyCardMapper;
import com.dsg.entity.account.DsgAccountCard;
import com.dsg.entity.apply.DsgApplyCard;
import com.dsg.entity.oil.DsgOilDataDetail;
import com.dsg.service.account.IDsgAccountCardService;
import com.dsg.util.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 油咭總帳 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-12
 */
@Service
public class DsgAccountCardServiceImpl extends ServiceImpl<DsgAccountCardMapper, DsgAccountCard> implements IDsgAccountCardService {

    @Autowired
    private DsgApplyCardMapper dsgApplyCardMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DsgAccountCard> getOrCreateDsgAccountCard(List<DsgOilDataDetail> dsgOilDataDetailList) {
        if (CollectionUtil.isEmpty(dsgOilDataDetailList)) {
            throw new RRException("上油數據不能為空！");
        }
        List<String> cardNoList = dsgOilDataDetailList.stream().map(DsgOilDataDetail::getCardNo).collect(Collectors.toList());
        Map<String, DsgOilDataDetail> dsgOilDataDetailMap = dsgOilDataDetailList.stream().collect(Collectors.toMap(I -> I.getCardNo(), Function.identity(), (oldData, newData) -> newData));
        QueryWrapper<DsgAccountCard> cardQueryWrapper = new QueryWrapper<>();
        cardQueryWrapper.in("CARD_NO", cardNoList);
        List<DsgAccountCard> dsgAccountCardList = list(cardQueryWrapper);
        Map<String, DsgAccountCard> dsgAccountCardMap = dsgAccountCardList.stream().collect(Collectors.toMap(I -> I.getCardNo(), Function.identity(), (oldData, newData) -> newData));
        List<DsgAccountCard> returnList = new ArrayList<>();
        List<DsgAccountCard> addList = new ArrayList<>();
        for (String key : dsgOilDataDetailMap.keySet()) {
            DsgOilDataDetail dsgOilDataDetail = dsgOilDataDetailMap.get(key);
            if (!dsgAccountCardMap.containsKey(dsgOilDataDetail.getCardNo())) {
                DsgAccountCard dsgAccountCard = new DsgAccountCard();
                dsgAccountCard.setOid(UUIDGenerator.getUUID());
                dsgAccountCard.setCardNo(dsgOilDataDetail.getCardNo());
                dsgAccountCard.setCusNo(dsgOilDataDetail.getCusNo());
                dsgAccountCard.setOilCom(dsgOilDataDetail.getOilCom());
                dsgAccountCard.setBalanceValue(BigDecimal.ZERO);
                dsgAccountCard.setArrearsValue(BigDecimal.ZERO);
                dsgAccountCard.setWaitPostValue(BigDecimal.ZERO);
                addList.add(dsgAccountCard);
            } else {
                returnList.add(dsgAccountCardMap.get(dsgOilDataDetail.getCardNo()));
            }
        }
        if (CollectionUtil.isNotEmpty(addList)) {
            saveBatch(addList);
            returnList.addAll(addList);
        }
        return returnList;
    }

    @Override
    public DsgAccountCard getAddDsgAccountCard(String cardNo) {
        if (StringUtil.isEmpty(cardNo)) {
            throw new RRException("參數錯誤:咭號不能為空！");
        }
        QueryWrapper<DsgAccountCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CARD_NO", cardNo);
        DsgAccountCard dsgAccountCard = getOne(queryWrapper, false);
        SystemAuthorizingUser user = SingletonLoginUtils.getSystemUser();
        DsgApplyCard dsgApplyCard = new DsgApplyCard();
        dsgApplyCard.setEntCode(user.getOrgCode());
        dsgApplyCard.setCardNo(cardNo);
        List<DsgApplyCard> dsgApplyCardList = dsgApplyCardMapper.selectApplyCardAndCusNo(dsgApplyCard);
        if (dsgAccountCard == null) {
            if (CollectionUtil.isNotEmpty(dsgApplyCardList) && StringUtil.isEmpty(dsgApplyCardList.get(0).getAccountCardOid())) {
                dsgAccountCard = new DsgAccountCard();
                dsgAccountCard.setOid(UUIDGenerator.getUUID());
                dsgAccountCard.setCardNo(cardNo);
                dsgAccountCard.setCusNo(dsgApplyCardList.get(0).getCusNo());
                dsgAccountCard.setOilCom(dsgApplyCardList.get(0).getOilCom());
                dsgAccountCard.setBalanceValue(BigDecimal.ZERO);
                dsgAccountCard.setArrearsValue(BigDecimal.ZERO);
                dsgAccountCard.setWaitPostValue(BigDecimal.ZERO);
                dsgAccountCard.setAccountName(dsgApplyCardList.get(0).getAccountName());
                dsgAccountCard.setAccountNo(dsgApplyCardList.get(0).getAccountNo());
                dsgAccountCard.setPayAccountNo(dsgApplyCardList.get(0).getPayAccountNo());
                dsgAccountCard.setPayAccountName(dsgApplyCardList.get(0).getPayAccountName());
                save(dsgAccountCard);
            }
        } else {
            if (CollectionUtil.isNotEmpty(dsgApplyCardList)) {
                dsgAccountCard.setAccountName(dsgApplyCardList.get(0).getAccountName());
                dsgAccountCard.setAccountNo(dsgApplyCardList.get(0).getAccountNo());
                dsgAccountCard.setPayAccountNo(dsgApplyCardList.get(0).getPayAccountNo());
                dsgAccountCard.setPayAccountName(dsgApplyCardList.get(0).getPayAccountName());
            }
        }
        return dsgAccountCard;
    }

    @Override
    public List<DsgAccountCard> selectUserAccountList(String cusNo) {
        return baseMapper.selectUserAccountList(cusNo, SingletonLoginUtils.getSystemEntCode());
    }

    @Override
    public List<DsgAccountCard> selectUserCardAccountList(String cusNo) {
        return baseMapper.selectUserCardAccountList(cusNo, SingletonLoginUtils.getSystemEntCode());
    }
}
